//
//  LYUserActionView.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/26.
//

import UIKit

protocol LYUserActionViewDelegate: AnyObject {
    func didSelected(actionView: LYUserActionView, model: LYActionModel?, didClick index: Int)
}

class LYUserActionView: UIView {
    
    weak var delegate: LYUserActionViewDelegate? = nil
    
    private lazy var backgroundImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = false
        iv.image = UIImage(named: "user_bg")
        return iv
    }()
   
    private lazy var stackView: UIStackView = {
        let stack = UIStackView()
        stack.axis = .horizontal
        stack.distribution = .fillEqually  // 子视图等宽
        stack.alignment = .fill
        stack.spacing = 5  // 设置子视图间距
        return stack
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(backgroundImageView)
        backgroundImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        addSubview(stackView)
        stackView.snp.makeConstraints { make in
            make.leading.trailing.equalToSuperview().inset(10)
            make.top.equalToSuperview().offset(45)
            make.bottom.equalToSuperview().offset(-10)
        }
    }
    
    func setActionView(models: [LYActionModel]) {
        for (index, model) in models.enumerated() {
            let actionView = LYImageActionView()
            actionView.tag = index
            actionView.model = model
            actionView.setData(imageName: model.imageName,
                               title: model.title)
            actionView.addTarget(self, action: #selector(clickActionView(_:)), for: .touchUpInside)
            stackView.addArrangedSubview(actionView)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func clickActionView(_ sender: LYImageActionView) {
        delegate?.didSelected(actionView: self, model: sender.model, didClick: sender.tag)
    }
}

struct LYActionModel {
    let imageName: String
    let title: String
}

class LYImageActionView: UIControl {
    
    var model: LYActionModel?
    
    private lazy var iconImageView: UIImageView = {
        let iv = UIImageView()
        iv.contentMode = .scaleAspectFit
        iv.clipsToBounds = false
        iv.image = UIImage(named: "user_bg")
        return iv
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        label.textColor = UIColor(hex: "#FFFFFF")
        label.textAlignment = .center
        label.text = "all"
        return label
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(iconImageView)
        addSubview(titleLabel)
        
        iconImageView.snp.makeConstraints { make in
            make.top.centerX.equalToSuperview()
            make.width.height.equalTo(32 * xScale)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(iconImageView.snp.bottom).offset(10)
            make.centerX.equalTo(iconImageView)
            make.leading.trailing.equalToSuperview()
        }
    }
    
    func setData(imageName: String, title: String) {
        self.iconImageView.image = UIImage(named: imageName)
        self.titleLabel.text = title
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
